package com.zhuoyue.biz.core.command.cmd.common.fe6;


import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;

import com.zhuoyue.biz.core.command.cmd.common.fe6.base.QuNuanQi0Fe60206Type;
import com.zhuoyue.biz.core.command.constant.QuNuanQi0Fe60206CmdDeal;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.utils.CmdUtils;


import java.util.Calendar;
import java.util.Map;

/**
 * 取暖器
 *
 * @author Administrator
 */
public class QuNuqanQi0Fe60206 {

    private static final String OPEN = "OPEN";
    private static final String CLOSE = "CLOSE";
    /**
     * 时间参数小于16说明是
     */
    private static final int HEX = 16;

    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String cmd = "";
        switch (controlReq.getControlType()) {
            case ControlType.SWITCH_STATE_REQUEST:
                cmd = getSwitchStopCmd(deviceAndGatewayDO, controlReq);
                break;
            case ControlType.INCREMENT_TEMPERATURE_REQUEST:
                cmd = getTemperatureCmd(deviceAndGatewayDO, controlReq);
                break;
            default:
                break;

        }


        return cmd;
    }


    /**
     * 开关停
     * 开关机命令
     *
     * @param deviceAndGatewayDO
     * @param controlReq
     * @return
     */
    private static String getSwitchStopCmd(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();
        Map<String, Object> actionsMap = controlReq.getActions();
        String state = actionsMap.get("state").toString();
//开关状态
        String cmd11 = "";
        if (OPEN.equals(state)) {
            cmd11 = QuNuanQi0Fe60206Type.ControlType.CMD_11_OPEN;
        } else if (CLOSE.equals(state)) {
            cmd11 = QuNuanQi0Fe60206Type.ControlType.CMD_11_CLOSE;
        }

        String hexTime = getHexTime();
        String cmdJtoL = QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_10 +
                //51或50
                cmd11 +
                //详细见Excel
                hexTime;
        String cmdCheckSum1 = CmdUtils.getCmdBodySum(cmdJtoL);
        String orderLength = CmdUtils.getDateLength(cmdJtoL);

        String cmdHtoN = QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_1 +
                orderLength +
                cmdJtoL +
                cmdCheckSum1 +
                //数据长度
                QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_14;

        String dateLength = CmdUtils.getDateLength(cmdHtoN);

        String cmdCn = QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_3 +
                QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_4 +
                gatewayMac +
                deviceMac +
                dateLength +
                cmdHtoN;

        String frameLength = CmdUtils.getDateLength(cmdCn);

        String cmdCheckSum2 = CmdUtils.getCmdBodySum(cmdCn);

        String fullCmd = QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_1 + frameLength + cmdCn + cmdCheckSum2 + QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_14;

        return fullCmd;
    }


    /**
     * 温度设置
     *
     * @param deviceAndGatewayDO
     * @param controlReq
     * @return
     */
    private static String getTemperatureCmd(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {

        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();
        Map<String, Object> actionsMap = controlReq.getActions();
        int temperature = (int) actionsMap.get("temperature");
        String valueStr = Integer.toHexString(temperature);
        String hexTime = getHexTime();
        String cmdJtoL = QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_10 +
                QuNuanQi0Fe60206Type.ControlType.CMD_11_MODE +
                //温度
                valueStr +
                //详细见Excel
                hexTime;
//校验1
        String cmdCheckSum1 = CmdUtils.getCmdBodySum(cmdJtoL);
        //计算指令长度
        String orderLength = CmdUtils.getDateLength(cmdJtoL);

        String cmdHtoN = QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_1 +
                orderLength +
                cmdJtoL +
                cmdCheckSum1 +
                QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_14;
//数据长度
        String dataLength = CmdUtils.getDateLength(cmdHtoN);

        String cmdCn = QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_3 +
                QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_4 +
                gatewayMac +
                deviceMac +
                dataLength +
                cmdHtoN;
//帧长度
        String frameLength = CmdUtils.getDateLength(cmdCn);

        String cmdCheckSum2 = CmdUtils.getCmdBodySum(cmdCn);

        String fullCmd = QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_1 +
                frameLength +
                cmdCn +
                cmdCheckSum2 +
                QuNuanQi0Fe60206CmdDeal.ControlCmd.CMD_14;


        return fullCmd;

    }


    /**
     * 获取时间
     *
     * @return
     */
    public static String getHexTime() {

        Calendar ca = Calendar.getInstance();
        ca.setTime(new java.util.Date());
        String year = "" + ca.get(Calendar.YEAR);
        Integer month = ca.get(Calendar.MONTH) + 1;
        Integer day = ca.get(Calendar.DATE);
        Integer week = ca.get(Calendar.DAY_OF_WEEK) - 1;
        Integer hour = ca.get(Calendar.HOUR_OF_DAY);
        Integer m = ca.get(Calendar.MINUTE);
        Integer s = ca.get(Calendar.SECOND);
        System.out.println(year + "  " + month + "  " + "  " + day + "   " + week + "  " + hour + "   " + m + "   " + s);
        //截取19
        Integer strh = Integer.parseInt(year.substring(year.length() - 2, year.length()));
        String yh = Integer.toHexString(strh);
        String mh = Integer.toHexString(month);
        String dh = null;
        String hh = null;
        String mih = null;
        String sh = null;
        if (day < HEX) {
            dh = "0" + Integer.toHexString(day);
        } else {
            dh = Integer.toHexString(day);
        }

        String wh = Integer.toHexString(week);

        if (hour < HEX) {
            hh = "0" + Integer.toHexString(hour);
        } else {
            hh = Integer.toHexString(hour);
        }

        if (m < HEX) {
            mih = "0" + Integer.toHexString(m);
        } else {
            mih = Integer.toHexString(m);
        }

        if (s < HEX) {
            sh = "0" + Integer.toHexString(s);
        } else {
            sh = Integer.toHexString(s);
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(yh).append(mh).append(wh).append(dh).append(hh).append(mih).append(sh);
        System.out.println(stringBuilder.toString());

        return stringBuilder.toString();
    }

}
